package com.borrow.jiemihua.push;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseConstant;
import com.borrow.jiemihua.mainUI.activity.PlatformDetailsActivity;
import com.borrow.jiemihua.utils.SharedPreferencesUtils;
import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;


public class PushIntentService extends GTIntentService {
    private static final int NOTIFICATION_FLAG = 1;
    private  PushContent pscontent=new PushContent();
    public PushIntentService() {

    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {

    }
    /**
     * 为了观察透传数据变化.
     */
    private static int cnt;
    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        try {
            byte []payload=msg.getPayload();
            String content=new String(payload,"utf-8");
            Log.d("==PUSH1",content);
//            Gson gson=new Gson();
//            PushContent pscontent22=gson.fromJson(content, PushContent.class);
//            pscontent=pscontent22;
//            showNotificationTC(content);

        }catch (Exception e){
        }
    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.d("===个推clientId",clientid);
        SharedPreferencesUtils.saveString(PushIntentService.this, BaseConstant.SPConstant.clientid, clientid);
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
//        Log.d("===个推通知11","----"+context);

    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {

    }

    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage message) {
        Log.d("===个推通知333", "-- -> " + "appid = " + message.getAppid() + "\ntaskid = " + message.getTaskId() + "\nmessageid = "
                + message.getMessageId() + "\npkg = " + message.getPkgName() + "\ncid = " + message.getClientId() + "\ntitle = "
                + message.getTitle() + "\ncontent = " + message.getContent());
        showNotification(message);
    }

    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage message) {
        Log.d("===个推通知444", "---> " + "appid = " + message.getAppid() + "\ntaskid = " + message.getTaskId() + "\nmessageid = "
                + message.getMessageId() + "\npkg = " + message.getPkgName() + "\ncid = " + message.getClientId() + "\ntitle = "
                + message.getTitle() + "\ncontent = " + message.getContent());
    }

    private Intent intent;
    public void showNotificationTC(String  pscontent){
        intent= new Intent();
        intent.setClass(this, PlatformDetailsActivity.class);
        PendingIntent pendingIntent= PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        Notification.Builder mBuilder = new Notification.Builder(this);
//        mBuilder.setContentTitle(pscontent.getTitle())//设置通知栏标题
//                .setContentText(pscontent.getDesc()) //设置通知栏显示内容
//                .setContentIntent(pendingIntent) //设置通知栏点击意图
//                //  .setNumber(number) //设置通知集合的数量
//                .setTicker(pscontent.getTitle()) //通知首次出现在通知栏，带上升动画效果的
//                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
//                .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
//                //  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
//                .setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
//                .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
//                //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
//                .setAutoCancel(true)
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .getNotification();//设置通知小ICON
        Notification notify2 = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.logo)
                .setTicker(pscontent.toString())
                .setContentTitle(pscontent.toString())
                .setContentText(pscontent.toString())
                .setContentIntent(pendingIntent)
                .getNotification();
        notify2.flags |= Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(0, notify2);

    }
    public void showNotification(GTNotificationMessage pscontent){
        Log.d("==PUSH2222",pscontent.toString());
        intent= new Intent();
        String con=pscontent.getContent();
        String id=con.substring(con.indexOf("="),con.length());
        intent.putExtra("terraceId",id);
//        Log.d("==PUSH333",id);
        if(con!=null){
            intent.setClass(this, PlatformDetailsActivity.class);
        }
        PendingIntent pendingIntent= PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        Notification.Builder mBuilder = new Notification.Builder(this);
//        mBuilder.setContentTitle(pscontent.getTitle())//设置通知栏标题
//                .setContentText(pscontent.getDesc()) //设置通知栏显示内容
//                .setContentIntent(pendingIntent) //设置通知栏点击意图
//                //  .setNumber(number) //设置通知集合的数量
//                .setTicker(pscontent.getTitle()) //通知首次出现在通知栏，带上升动画效果的
//                .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
//                .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
//                //  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
//                .setOngoing(false)//ture，设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
//                .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合
//                //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
//                .setAutoCancel(true)
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .getNotification();//设置通知小ICON
        Notification notify2 = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.logo)
                .setTicker(pscontent.getTitle())
                .setContentTitle(pscontent.getTitle())
//                .setContentText(pscontent.getContent())
                .setContentIntent(pendingIntent)
                .getNotification();
        notify2.flags |= Notification.FLAG_AUTO_CANCEL;
        mNotificationManager.notify(0, notify2);

    }



}

